package com.example.parse_springboot.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.parse_springboot.config.jwt.JWTUtils;
import com.example.parse_springboot.entity.User;
import com.example.parse_springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper mapper;

    public String loginService(String username, String rawp) throws Exception {
        String password = DigestUtils.md5DigestAsHex(rawp.getBytes());

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername, username);
        User user = mapper.selectOne(wrapper);
        if (user == null) {
            throw new Exception("用户不存在");
        }
        if (!password.equals(user.getPassword())) {
            throw new Exception("密码错误");
        }

        Map<String, String> map = new HashMap<>();
        map.put("id", user.getId().toString());
        map.put("username", user.getUsername());
        String token = JWTUtils.getToken(map);

        return token;
    }

    public User register(String username, String rawp) throws Exception {
        String password = DigestUtils.md5DigestAsHex(rawp.getBytes());

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername, username);
        User exUser = mapper.selectOne(wrapper);
        if (exUser != null) {
            throw new Exception("用户名已存在");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int insert = mapper.insert(user);
        if (insert <= 0) {
            throw new Exception("插入数据库失败");
        }
        return user;
    }

    public String updatePassword(String username, String oldRawPassword, String newRawPassword) throws Exception {
        String oldPassword = DigestUtils.md5DigestAsHex(oldRawPassword.getBytes());
        String newPassword = DigestUtils.md5DigestAsHex(newRawPassword.getBytes());

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername, username);
        User user = mapper.selectOne(wrapper);
        if (user == null) {
            throw new Exception("用户不存在");
        }
        if (!oldPassword.equals(user.getPassword())) {
            throw new Exception("旧密码错误");
        }

        user.setPassword(newPassword);
        int update = mapper.updateById(user);
        if (update <= 0) {
            throw new Exception("密码更新失败");
        }

        return "密码更新成功";
    }

    public User getUserInfo(String username) throws Exception {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername, username);
        User user = mapper.selectOne(wrapper);
        if (user == null) {
            throw new Exception("用户不存在");
        }
        return user;
    }

    public String deleteUser(String username) throws Exception {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername, username);
        User user = mapper.selectOne(wrapper);
        if (user == null) {
            throw new Exception("用户不存在");
        }

        int delete = mapper.delete(wrapper);
        if (delete <= 0) {
            throw new Exception("用户删除失败");
        }

        return "用户删除成功";
    }

    public String resetPassword(String username, String newRawPassword) throws Exception {
        String newPassword = DigestUtils.md5DigestAsHex(newRawPassword.getBytes());

        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getUsername, username);
        User user = mapper.selectOne(wrapper);
        if (user == null) {
            throw new Exception("用户不存在");
        }

        user.setPassword(newPassword);
        int update = mapper.updateById(user);
        if (update <= 0) {
            throw new Exception("密码重置失败");
        }

        return "密码重置成功";
    }
}
